<#assign menuId=7>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>新增工厂-${Application.systemName!}</title>
        <meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
        <!-- bootstrap 3.0.2 -->
        <link href="../css/bootstrap.min.css" rel="stylesheet" type="text/css" />
        <!-- font Awesome -->
        <link href="../css/font-awesome.min.css" rel="stylesheet" type="text/css" />
        <!-- Ionicons -->
        <link href="../css/ionicons.min.css" rel="stylesheet" type="text/css" />
        <!-- fullCalendar -->
        <link href="../css/fullcalendar/fullcalendar.css" rel="stylesheet" type="text/css" />
        <link href="../css/fullcalendar/fullcalendar.print.css" rel="stylesheet" type="text/css" media='print' />
        <!-- Theme style -->
        <link href="../css/AdminLTE.css" rel="stylesheet" type="text/css" />
        <!-- bootstrapt表单验证 -->
		<link rel="stylesheet" href="../css/bootstrapValidator.min.css"/>

		<link rel="stylesheet" href="../css/myself.css"/>
		
		<style type="text/css">
			.star {
				color: red;
			}
			
			.special {
				border: 1px solid black;
			}
			
			.style {
				cursor: pointer;
			}
			
			.style label {
				width: 15%;
				cursor: pointer;
			}
			
			.table>thead>tr>th, .table>tbody>tr>th, .table>tfoot>tr>th, .table>thead>tr>td,
				.table>tbody>tr>td, .table>tfoot>tr>td {
				vertical-align: middle;
			}
			
			.table tr td:nth-child(1) {
				min-width: 100px;
				text-align: center;
			}
			/*新增样式*/
			ol {
				list-style: none;
			}
			
			.ol_left {
				width: 100%;
				height: 30px;
				line-height: 30px;
				padding: 0;
			}
			
			.ol_left>li {
				float: left;
				margin-right: 5px;
			}
			
			.ol_left li, .ol_left {
				margin-bottom: 0;
			}
			
			#factoryName {
				border: 1px solid #ccc;
				padding: 6px;
				width: 28%;
			}
			
			select {
				padding: 6px;
				width: 100px;
			}
			
			.special {
				border: 1px solid #ccc;
				width: 100%;
				padding: 6px;
			}
			
			.special:nth-child(1) {
				width: 40%;
				margin-top: 5px;
			}
			
			/* 上传文件样式 */
			.file {
				position: relative;
				display: inline-block;
				background: #D0EEFF;
				border: 1px solid #99D3F5;
				border-radius: 4px;
				padding: 4px 12px;
				overflow: hidden;
				color: #1E88C7;
				text-decoration: none;
				text-indent: 0;
				line-height: 20px;
			}
			
			.file input {
				position: absolute;
				font-size: 100px;
				right: 0;
				top: 0;
				opacity: 0;
			}
			
			.file:hover {
				background: #AADFFD;
				border-color: #78C3F3;
				color: #004974;
				text-decoration: none;
			}
			
			.file_name {
				display: inline-block;
				width: auto;
				height: 30px;
				line-height: 30px;
				color: #3882FF;
				position: relative;
				top: -10px;
			}
			
			.file_fa {
				position: relative;
			}
			
			.file_fa button {
				position: absolute;
			}
			
			#fileName {
				color: #3882ff;
				font-size: 14px;
				display: inline-block;
				line-height: -20px;
			}
			/* bootstrapt表单验证错误成功颜色控制样式 */
			.form-group.has-success label{
    			color: #000;
			}
			.form-group.has-error label{
    			color: #000;
			}
</style>
    </head>
    <body class="skin-blue">
        <!-- header logo: style can be found in header.less -->
        <#include "../header.html"/>
        <div class="wrapper row-offcanvas row-offcanvas-left">
            <!-- Left side column. contains the logo and sidebar -->
            <#include "../menu.html"/>
            <!-- Right side column. Contains the navbar and content of the page -->
            <aside class="right-side">
            	<section class="content-header">
                    <h1>新增工厂</h1>
                    <ol class="ol_left">
		                <li>当前位置：</li>
		                <li><a href="list">工厂管理</a></li>
		                <li>> 新建工厂</li>
		            </ol>
		            <hr class="no-margin" />
                </section>
                <!-- Main content -->
                <section class="content" >
                    <div class="row">
                        <div class="col-md-12">
                            <div class="box" style="padding: 20px;">
                                
                                <div class="box-title center">
                                <form id="add_factory" action="/jhzz_scm/factory/insertFactory" method="post" enctype="multipart/form-data">
                                	<table class="table table-bordered">
                                    	<tr>
                                    		<td width="10%">
                                    			<span class="star">*</span>
                                    			<span>工厂名称</span>
                                    		</td>
                                    		<td colspan="5">
                                    			<div class="form-group">
                                    				<input id="factoryName" name="factoryName" type="text" placeholder="请输入工厂名称" />
                                    			</div>
                                    		</td>
                                    	</tr>
                                    	<tr>
                                    		<td>
                                    			<span class="star">*</span>
                                    			<span>工厂地址</span>
                                    		</td>
                                    		<td colspan="5">
                                    			<div style="overflow: hidden;">
													<div class="form-group" style="float: left; margin-right: 5px;">
														<select id="province" name="provinceId">
															<option value="">请选择</option>
														</select>
													</div>
													<div class="form-group" style="float: left; margin-right: 5px;">
														<select id="city" name="cityId">
															<option value="">请选择</option>
														</select>
													</div>
													<div class="form-group" style="float: left;">
														<select id="area" name="areaId">
															<option value="">请选择</option>
														</select>
													</div>
												</div>
												<div class="form-group">
													<input id="detailAddress" name="detailAddress" class="address special" type="text" placeholder="填写详细地址" />
												</div>
                                    		</td>
                                    	</tr>
                                    	<tr>
                                    		<td>
                                    			<span class="star">*</span>
                                    			<span>联系人</span>
                                    		</td>
                                    		<td>
                                    			<div class="form-group">
                                    				<input id="linkman" name="linkman" class="special" type="text"  />
                                    			</div>
                                    		</td>
                                    		<td>
                                    			<span class="star">*</span>
                                    			<span>联系电话</span>
                                    		</td>
                                    		<td colspan="3">
                                    			<div class="form-group">
                                    				<input id="contact" name="contact" class="special" type="text" />
                                    			</div>
                                    		</td>
                                    	</tr>
                                    	<tr>
                                    		<td>
                                    			<span class="star">*</span>
                                    			<span>工厂人数</span>
                                    		</td>
                                    		<td colspan="5">
                                    			<div id="personAmount" class="style form-group">
                                    				
                                    			</div>
                                    		</td>
                                    	</tr>
                                    	<tr>
                                    		<td>
                                    			<span class="star">*</span>
                                    			<span>梭织类型</span>
                                    		</td>
                                    		<td colspan="5">
                                    			<div id="tattingStyle" class="style form-group" style="margin-bottom: 15px;">
                                    				
                                    			</div>
                                    		</td>
                                    	</tr>
                                    	<tr>
                                    		<td>
                                    			<span class="star">*</span>
                                    			<span>针织类型</span>
                                    		</td>
                                    		<td colspan="5">
                                    			<div id="wovenStyle" class="style form-group" style="margin-bottom: 15px;">
                                    				
                                    			</div>
                                    		</td>
                                    	</tr>
                                    	<tr>
                                    		<td>
                                    			<span class="star">*</span>
                                    			<span>毛衣类型</span>
                                    		</td>
                                    		<td colspan="5">
                                    			<div id="sweaterStyle" class="style form-group" style="margin-bottom: 15px;">
                                    				
                                    				
                                    			</div>
                                    		</td>
                                    	</tr>
                                    	<tr>
                                    		<td>
                                    			<span class="star">*</span>
                                    			<span>销往目的地</span>
                                    		</td>
                                    		<td colspan="5">
                                    			<div id="saleTarget" class="form-group">
                                    			</div>
                                    		</td>
                                    	</tr>
                                    	<tr>
                                    		<td>
                                    			<span class="star">*</span>
                                    			<span>合作条件</span>
                                    		</td>
                                    		<td colspan="5">
                                    			<div id="cooperationCondition" class="style form-group">
                                    			
                                    			</div>
                                    		</td>
                                    	</tr>
                                    	<tr>
                                    		<td>
                                    			<span class="star">*</span>
                                    			<span>工厂评估</span>
                                    		</td>
                                    		<td colspan="5">
												
												<div class="form-group no-margin">
		                                            <div class="file_fa">
                                    					<a href="javascript:;" class="file">
														    <input id="estimateReport" name="estimateReport" type="file"  multiple  onchange="document.getElementById('fileName').innerHTML=this.value">点击选择文件
														</a>
														<span id="fileName"></span>
                                    				</div>
		                                        </div>
                                    		</td>
                                    	</tr>
                                   </table>
                                   </form>
                                   	<div style="margin-bottom: 50px;">
				                		<button id="add" type="button" class="btn btn-primary">确定新建</button>
					                	<button id="clean" type="button" class="btn btn-default">放弃新建</button>
					                </div>
                                </div>
                            </div><!-- /.box -->

                            
                        </div><!-- /.col -->
                        
                    </div><!-- /.row -->
                    
                </section><!-- /.content -->
                
            </aside><!-- /.right-side -->
        </div><!-- ./wrapper -->


        <!-- jQuery 2.0.2 -->
        <script src="../js/jquery.min.js"></script>
        <!-- jQuery UI 1.10.3 -->
        <script src="../js/jquery-ui-1.10.3.min.js" type="text/javascript"></script>
        <script type="text/javascript">
        
        $(function(){
        	//获取所有省数据
        	getAllProvince();
        	$('#province').change(provinceOnSelect);
        	$('#city').change(cityOnSelect);
        	//获取工厂人数列表
        	getPersonAmount();
        	//获取梳织类型列表
        	getTattingStyle();
        	//获取针织类型列表
        	getWovenStyle();
        	//获取毛衣类型列表
        	getSweaterStyle();
        	//获取销往目的地列表
        	getSaleTarget();
        	//获取合作条件
        	getCooperationCondition();
        	//新建按钮点击事件
        	$('#add').click(addFactory);
        	//清除表单数据
        	$('#clean').click(cleanForm);
        	//表单验证
        	checkForm();
        });

        //获取所有省数据
        function getAllProvince(){
        	var url = "../area/province";
        	$.getJSON(url,function(result){
        		if(result.head.respCode=="0000000"){
        			var provinces = result.body.rows;
        			var view = $('#province');
        			view.empty();
        			var option = $('<option value="">请选择</option>');
        			view.append(option)
        			for(var i=0;i<provinces.length;i++){
        				var option = $('<option value="'+provinces[i].areaId+'">'+provinces[i].areaName+'</option>');
        				view.append(option);
        			}
        		}
        	});
        }

        //从服务端根据parentId获取对应的市列表
        function getCityByParentId(parentId){
        	//console.log(parentId);
        	var url = "../area/cityOrArea";
        	var data = {"parentId":parentId};
        	$.getJSON(url,data,function(result){
        		if(result.head.respCode=="0000000"){
        			var citys = result.body.rows;
        			var view = $('#city');
        			view.empty();
        			var option = $('<option value="">请选择</option>');
        			view.append(option)
        			for(var i=0;i<citys.length;i++){
        				var option = $('<option value="'+citys[i].areaId+'">'+citys[i].areaName+'</option>');
        				view.append(option);
        			}
        		}
        	});
        }

        //从服务端根据parentId获取对应的区县列表
        function getAreaByParentId(parentId){
        	//console.log(parentId);
        	var url = "../area/cityOrArea";
        	var data = {"parentId":parentId};
        	$.getJSON(url,data,function(result){
        		if(result.head.respCode=="0000000"){
        			var areas = result.body.rows;
        			var view = $('#area');
        			view.empty();
        			var option = $('<option value="">请选择</option>');
        			view.append(option)
        			for(var i=0;i<areas.length;i++){
        				var option = $('<option value="'+areas[i].areaId+'">'+areas[i].areaName+'</option>');
        				view.append(option);
        			}
        		}
        	});
        }

        //省下拉框选中监听事件
        function provinceOnSelect(){
        	if($(this).val()!=""){
        		//省选中后清空所有
        		cleanArea();
        		var provinceId = $(this).val();
        		//从服务端获取对应的城市列表
        		getCityByParentId(provinceId);
        	}
        	if($(this).val()==""){
        		cleanCity();
        		cleanArea();
        	}
        }

        //市下拉框选中监听事件
        function cityOnSelect(){
        	if($(this).val()!=""){
        		var cityId = $(this).val();
        		//从服务端获取对应的区县列表
        		getAreaByParentId(cityId);
        	}
        	if($(this).val()==""){
        		cleanArea();
        	}
        }


        //清空市
        function cleanCity(){
        	var view = $('#city');
        	view.empty();
        	var option = $('<option value="">请选择</option>');
        	view.append(option);
        }

        //清空区县
        function cleanArea(){
        	var view = $('#area');
        	view.empty();
        	var option = $('<option value="">请选择</option>');
        	view.append(option);
        }

        //获取工厂人数列表
        function getPersonAmount(){
        	var constants = getConstant("scm_factory-person_amount");
        	var view = $('#personAmount');
        	view.empty();
        	for(var i=0;i<constants.length;i++){
        		var label = $('<label><input type="radio" name="personAmount" value="'+constants[i].name+'"  />'+constants[i].name+'</label>')
        		view.append(label);
        	}
        }

        //获取梳织类型列表
        function getTattingStyle(){
        	var constants = getConstant("scm_factory-tatting_style");
        	var view = $('#tattingStyle');
        	view.empty();
        	for(var i=0;i<constants.length;i++){
        		var label = $('<label><input type="checkbox" name="tattingStyleIds" value="'+constants[i].id+'"  />'+constants[i].name+'</label>')
        		view.append(label);
        	}
        }

        //获取针织类型列表
        function getWovenStyle(){
        	var constants = getConstant("scm_factory-woven_style");
        	var view = $('#wovenStyle');
        	view.empty();
        	for(var i=0;i<constants.length;i++){
        		var label = $('<label><input type="checkbox" name="wovenStyleIds" value="'+constants[i].id+'"  />'+constants[i].name+'</label>')
        		view.append(label);
        	}
        }

        //获取毛衣类型列表
        function getSweaterStyle(){
        	var constants = getConstant("scm_factory-sweater_style");
        	var view = $('#sweaterStyle');
        	view.empty();
        	for(var i=0;i<constants.length;i++){
        		var label = $('<label><input type="checkbox" name="sweaterStyleIds" value="'+constants[i].id+'"  />'+constants[i].name+'</label>')
        		view.append(label);
        	}
        }

        //获取销往目的地列表
        function getSaleTarget(){
        	var constants = getConstant("scm_factory-sale_target");
        	var view = $('#saleTarget');
        	view.empty();
        	for(var i=0;i<constants.length;i++){
        		var label = $('<label><input type="radio" name="saleTarget" value="'+constants[i].name+'"  />'+constants[i].name+'</label>')
        		view.append(label);
        	}
        }

        //获取合作条件
        function getCooperationCondition(){
        	var constants = getConstant("scm_factory-cooperation_condition");
        	var view = $('#cooperationCondition');
        	view.empty();
        	for(var i=0;i<constants.length;i++){
        		var label = $('<label><input type="radio" name="cooperationCondition" value="'+constants[i].name+'"  />'+constants[i].name+'</label>')
        		view.append(label);
        	}
        }

        //根据分类条件查询所有的常量列表
        function getConstant(category){
        	var constants={};
        	var url = "../constant/getConstantByCategory";
        	var data = {"category":category};
        	//关闭ajax异步
        	$.ajaxSettings.async = false;
        	$.getJSON(url,data,function(result){
        		if(result.head.respCode="0000000"){
        			constants = result.body.rows;
        		}
        	});
        	//开启ajax异步
        	$.ajaxSettings.async = true;
        	return constants;
        }

        //新增工厂
        function addFactory(){
        	$('#add_factory').bootstrapValidator('validate');
        	var bootstrapValidator = $("#add_factory").data('bootstrapValidator');
        	if(!bootstrapValidator.isValid()){
        		return;
        	}
        	var formData = new FormData($("#add_factory")[0]);  
            $.ajax({  
                 url: '../factory/insertFactory' ,  
                 type: 'POST',  
                 data: formData,  
                 async: false,  
                 cache: false,  
                 contentType: false,  
                 processData: false,  
                 success: function (result) {
                	 if(result.head.respCode=="0000000"){
                		 success(result.body,function(flag){
                		        if(flag){
                		        	window.location.href="../factory/list";
                		        }else if(flag){
                		        	$("#add_factory")[0].reset();
                					//adminLTE提供的复选框和单选框的选中状态清除方法
                					$("input[type='checkbox']:not(.simple), input[type='radio']:not(.simple)").iCheck({
                				        checkboxClass: 'icheckbox_minimal',
                				        radioClass: 'iradio_minimal'
                				    });
                					$('#fileName').empty();
                		        }
                		    },1,1);
                	 }else{
                		 alert(result.body);
                	 }
                 }
            });
        }
        //清空表单数据
        function cleanForm(){
        	confirm("放弃新建,本次数据将不进行保存",function(flag){
		        if(flag){
		        	window.location.href="../factory/list";
		        }
		    });
        }
        
        //表单验证
        function checkForm(){
        	$('#add_factory').bootstrapValidator({
        		  fields: {
        			  //工厂名称
        			  factoryName: {
        				  trigger:'blur',
        		          validators: {
        		              notEmpty: {
        		                  message: '工厂名称不能为空'
        		              },
        		              callback:{
        		            	  message: '工厂已存在',
        		            	  callback: function(value, validator){
        		            			if(!value) return true;
        		            			var unused=true;
        		            			var data={name:value};
        		            			$.ajax({
        		            				url:'../factory/haveName',
        		            				type:'get',
        		            				data:data,
        		            				dataType:'json',
        		            				async:false,
        		            				success:function(result){
        		            					if(result.head.respCode=='9999990'){
        		            						if(result.body){
        		            							unused=false;
        		            						}
        		            					}
        		            				}
        		            			});
        		            			return unused;
        		            		}
        		              } 
        		          }
        		      },
        		      //省
        		      provinceId: {
        		    	  trigger:'change',
        		          validators: {
        		        	  notEmpty: {
        		        		  message: '省地址不能为空',
        		              }
        		          }
        		      },
        		      //市
        		      cityId: {
        		    	  trigger:'change',
        		          validators: {
        		        	  notEmpty: {
        		        		  message: '市地址不能为空',
        		              }
        		          }
        		      },
        		      //区
        		      areaId: {
        		    	  trigger:'change',
        		          validators: {
        		        	  notEmpty: {
        		        		  message: '区县地址不能为空',
        		              }
        		          }
        		      },
        		      //详细地址
        		      detailAddress: {
        		    	  trigger:'blur',
        		          validators: {
        		        	  notEmpty: {
        		        		  message: '详细地址不能为空',
        		              }
        		          }
        		      },
        		      //联系人
        		      linkman: {
        		    	  trigger:'blur',
        		          validators: {
        		        	  notEmpty: {
        		        		  message: '联系人不能为空',
        		              }
        		          }
        		      },
        		      //联系方式
        		      contact: {
        		    	  trigger:'blur',
        		          validators: {
        		        	  notEmpty: {
        		        		  message: '联系电话不能为空',
        		              },
        		              regexp: {
        	                        regexp: /^(0\d{2,3}-?\d{7,8})|(1\d{10})$/,
        	                        message: '联系方电话不符规则'
        	                  }
        		          }
        		      },
        		      //工厂人数
        		      personAmount: {
       		        	  trigger:'ifChecked ifUnchecked',
           		          validators: {
           		        	  notEmpty: {
           		        		  message: '工厂人数不能为空',
           		              }
           		          }
        		      },
        		      //梭织类型
        		      tattingStyleIds: {
        		    	  trigger:'ifChecked ifUnchecked',
        		          validators: {
        		        	  notEmpty: {
        		        		  message: '梭织类型不能为空',
        		              }
        		          }
        		      },
        		      //针织类型
        		      wovenStyleIds: {
        		    	  trigger:'ifChecked ifUnchecked',
        		          validators: {
        		        	  notEmpty: {
        		        		  message: '针织类型不能为空',
        		              }
        		          }
        		      },
        		      //毛衣类型
        		      sweaterStyleIds: {
        		    	  trigger:'ifChecked ifUnchecked',
        		          validators: {
        		        	  notEmpty: {
        		        		  message: '毛衣类型不能为空',
        		              }
        		          }
        		      },
        		      //销往目的地
        		      saleTarget: {
        		    	  trigger:'ifChecked ifUnchecked',
        		          validators: {
        		        	  notEmpty: {
        		        		  message: '销往目的地不能为空',
        		              }
        		          }
        		      },
        		      //合作条件
        		      cooperationCondition: {
        		    	  trigger:'ifChecked ifUnchecked',
        		          validators: {
        		        	  notEmpty: {
        		        		  message: '合作条件不能为空',
        		              }
        		          }
        		      },
        		      //工厂评估报告
        		      estimateReport: {
        		    	  trigger:'change',
        		          validators: {
        		        	  notEmpty: {
        		        		  message: '工厂评估报告不能为空',
        		              }
        		          }
        		      }
        		  }
        	});
        }

        </script>
        
        <!-- Bootstrap -->
        <script src="../js/bootstrap.min.js" type="text/javascript"></script>
        <!-- AdminLTE App -->
        <script src="../js/AdminLTE/app.js" type="text/javascript"></script>
        <!-- bootstrapt表单验证 -->
        <script type="text/javascript" src="../js/bootstrapValidator.min.js"></script>
    </body>
</html>
